
<?php
include '../template.php';
include '../dbconfig/config.php';
include '../dbconfig/opendb.php';
include 'blastparser.php';
include 'blast_fun.php';
include '../util/dbutil.php';
include '../util/pageutil.php';
getTop("..");
?>

&nbsp;<br />
	
<?php
	if(!isset($_POST['blast'])&&!isset($_GET['page'])) {
		die('Please input protein sequence!');
	}

        //$wd = "/home/p3db/www/blast/";
        $wd = "./";
        //$db = "phosphoproteins";

	if (isset($_POST['blast'])) { // first
		$seq = $_POST['seq'];
                $seq = blastseqpack($seq);

		$ecut = $_POST['evalue'];
                $org = $_POST['org'];
                $ref = $_POST['ref'];
	
		// by default we show first page
		$pageNum = 1;
	} else { //if(isset($_GET['page']))
                $seq = $_GET['seq'];
                $ecut = $_GET['evalue'];
		$pageNum = $_GET['page'];
                $org = $_GET['org'];
                $ref = $_GET['ref'];
                $blastres = $_GET['blastres'];
	}
        
        if (strlen($seq)<5) {
                echo "Please input a query protein sequence with length &ge;5";
                die();
        }

        if (isset($blastres)) {
            $data = unserializeFromDb($blastres);
            if (count($data)==1)
                $hits = $data[0];
        }

        if (!isset($hits)) {
            $blastout = run_blast_protein($wd,$seq,$ecut,$org,$ref);
            //parse the blast result to get gi number and evalue
            $hits = array();
            $lines = file($blastout);
            while (current($lines)) {
                    $line = current($lines);
                    if (strstr(line,"***** No hits found ******")) {
                            break;
                    }
                    if (strstr($line,"Sequences producing significant alignments:")) {
                            next($lines);
                            next($lines);
                            while (current($lines)) {
                                    $line = trim(current($lines));
                                    if (!empty($line)) { // hits
                                            $id = substr($line,0,strpos($line," "));
                                            //$gi = trim($gi); // remove the space in the beginning or the end
                                            $evalue = substr($line,strrpos($line," "));
                                            if ($hits[$id]==null) { // if already exists
                                                    $hits[$id] = $evalue;
                                            }
                                    } else {
                                            break;
                                    }
                                    next($lines);
                            }
                            break;
                    }
                    next($lines);
            } // end parse
            unlink($blastout);
            $ip = getRealIpAddr();
            $blastres = str_replace('/', '', $blastout).'.'.$ip.'.'.time();
            serializeToDb($blastres,$hits);
        }

	$get = "evalue=$ecut&org=$org&ref=$ref&blastres=$blastres&seq=$seq";

        $getref = "&org=$org&ref=$ref";
	
	// search database
		
	// how many rows to show per page
	$rowsPerPage = 20;

	// counting the offset
	$offset = ($pageNum - 1) * $rowsPerPage;
	
	$count = count($hits);
	if ($count==0) {
		echo "No significant hit was found (E-value&le;$ecut). Please incease the E-value cut-off and try again.";
	} else { //>0
		$ids = array_keys($hits);

                echo "<table width=\"100%\" border=\"0\"><tr>";
                echo "<td><font size=\"4\">$count phosphoproteins matched (E-value&le;$ecut):</font></td>";
                echo "</tr></table>";

		echo "<table border=\"1\" width=\"100%\">";
		echo "<tr><td width=\"100\"><b>Accession<b></td><td width=\"300\"><b>Description<b></td><td width=\"140\"><b>Source organism</b></td><td width=\"60\"><b>E-value</b></td></tr>";
		//foreach($hits as $gi=>$evalue) {
		for ($i=$offset; $i<$count && $i<$offset+$rowsPerPage; $i++) {
			$id = $ids[$i];
			$evalue = $hits[$id];
			$query  = "SELECT id, annotation, latin ".
                                  "FROM protein,organism WHERE protein.organism=organism.label AND protein.id=$id;";
			$result = mysql_query($query) or die($query.'--Error, query failed');
			while ($row = mysql_fetch_array($result)) {
                                $xref = implode('<br/>', getXref($id));
				echo "<tr><td><a href=\"../data/protein.php?id=$id$getref\">$xref</a></td><td>{$row['annotation']} </td><td><i>{$row['latin']}</i></td><td>$evalue</td></tr>";
			}
		}
		echo "</table>";
	}
	
	// how many pages we have when using paging?
	$maxPage = ceil($count/$rowsPerPage);
        $self = $_SERVER['PHP_SELF'];
        printPageNav($maxPage,"$self?$get&",$pageNum);

        if ($count>0) {
          //$blastouturl = "./tmp/".basename($blastout);
          echo "<table width=\"100%\"><tr><td align=\"right\"><table><tr>";
          //echo "<td><form method=\"post\" action=\"$blastouturl\"><input name=\"res\" type=\"submit\" id=\"res\" value=\"Get BLAST alignment file\"></form ></td>";
          echo "<td align=\"right\"><form method=\"post\" action=\".\"><input name=\"new\" type=\"submit\" id=\"new\" value=\"New inquiry\"></form></td></tr></table>";
          echo "</td></tr></table>";
        }

	include '../dbconfig/closedb.php';
?>


<?php getBottom(); ?>
